﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Designpatterns
{
    /// <summary>
    /// 运算类类 --简单工厂模式
    /// </summary>
    public class Operation
    {
        //二
        //public static double GetResult(double numbera, double numberb, string operate) 
        //{
        //    double result = 0d;
        //    switch (operate)
        //    {
        //        case "+":
        //            result = numbera + numberb;
        //            break;
        //        case "-":
        //            result = numbera - numberb;
        //            break;
        //        case "*":
        //            result = numbera * numberb;
        //            break;
        //        case "/":
        //            result = numbera / numberb;
        //            break;
        //        default:
        //            break;
        //    }
        //    return result;
        //}


        private double _numbera = 0;
        private double _numberb = 0;

        public  double NumebrA { get { return _numbera; } set { _numbera = value; } }
        public double NumebrB { get { return _numberb; } set { _numberb = value; } }

        public virtual double GetResult() 
        {
            double result = 0;
            return result;
        }
    }


    //加法1
    public class OperationAdd : Operation 
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumebrA + NumebrB;
            return result;
        }
    }
    //减法
    public class OperationSub : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumebrA - NumebrB;
            return result;
        }
    }
    //乘法
    public class OperationMul : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumebrA * NumebrB;
            return result;
        }
    }
    //除法
    public class OperationDiv : Operation
    {
        public override double GetResult()
        {
            double result = 0;

            if (NumebrB == 0)
                throw new Exception("除数不能为0");
            result = NumebrA / NumebrB;
            return result;
        }
    }

    /// <summary>
    /// 简单运算工厂类
    /// </summary>
    public class OpeerationFactory 
    {
        public static Operation createOperate(string operate) 
        {
            Operation oper = null;
            switch (operate)
            {
                case "+":
                    oper = new OperationAdd();
                    break;
                case "-":
                    oper = new OperationSub();
                    break;
                case "*":
                    oper = new OperationMul();
                    break;
                case "/":
                    oper = new OperationDiv();
                    break;

                default:
                    break;
            }
            return oper;
        }
    }



}
